package com.gx.recruitment.controller;

import com.gx.recruitment.entity.*;
import com.gx.recruitment.service.UserResumeService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class UserResumeController {
    @Resource
    private UserResumeService userResumeService;

    @PostMapping("/userresume/update")
    public Map<String, Object> updateResume(
            String resumername,
            String resumename,
            String resumesex,
            Date resumebirth,
            String resumeaddress,
            String resumeedu,
            String resumeexpr,
            String resumephone,
            Integer userid,
            String resumeemail,
            String jobname,
            String jobindustry,
            String jobaddress,
            String jobtype,
            String jobpay,
            Integer resumeid
    ) {
        Map<String, Object> map = new HashMap<>();
        User_resume user_resume = new User_resume();
        user_resume.setResumeRname(resumername);
        user_resume.setResumeName(resumename);
        user_resume.setResumeSex(resumesex);
        user_resume.setResumeBirth(resumebirth);
        user_resume.setResumeAddress(resumeaddress);
        user_resume.setResumeEdu(resumeedu);
        user_resume.setResumeExpr(resumeexpr);
        user_resume.setResumePhone(resumephone);
        user_resume.setUserId(userid);
        user_resume.setResumeEmail(resumeemail);
        user_resume.setJobName(jobname);
        user_resume.setJobIndustry(jobindustry);
        user_resume.setJobAddress(jobaddress);
        user_resume.setJobType(jobtype);
        user_resume.setJobPay(jobpay);
        user_resume.setResumeId(resumeid);

        int res = userResumeService.updateByPrimaryKey(user_resume);
        if (res == 0) {
            map.put("status", 200);
            map.put("msg", "修改成功");
        } else {
            map.put("msg", "修改失败");
        }
        return map;
    }

    @PostMapping("/userresume/add")
    public Map<String, Object> insertResume(
            String resumername,
            String resumename,
            String resumesex,
            Date resumebirth,
            String resumeaddress,
            String resumeedu,
            String resumeexpr,
            String resumephone,
            Integer userid,
            String resumeemail,
            String jobname,
            String jobindustry,
            String jobaddress,
            String jobtype,
            String jobpay

    ){
        Map<String,Object> map = new HashMap<>();
        User_resume user_resume = new User_resume();
        user_resume.setResumeRname(resumername);
        user_resume.setResumeName(resumename);
        user_resume.setResumeSex(resumesex);
        user_resume.setResumeBirth(resumebirth);
        user_resume.setResumeAddress(resumeaddress);
        user_resume.setResumeEdu(resumeedu);
        user_resume.setResumeExpr(resumeexpr);
        user_resume.setResumePhone(resumephone);
        user_resume.setUserId(userid);
        user_resume.setResumeEmail(resumeemail);
        user_resume.setJobName(jobname);
        user_resume.setJobIndustry(jobindustry);
        user_resume.setJobAddress(jobaddress);
        user_resume.setJobType(jobtype);
        user_resume.setJobPay(jobpay);

        User_resumeExample example = new User_resumeExample();
        User_resumeExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userid);
        List<User_resume> list = userResumeService.selectByUserId(example);
        if (list.size()>0){
            map.put("msg","简历已经存在");
        }else {
            int res = userResumeService.insertResume(user_resume);
            if(res == 0){
                map.put("status", 200);
                map.put("msg","创建成功");
            }else {
                map.put("msg","创建失败");
            }
        }
        return map;
    }

    @PostMapping("/userresume/deleteone")
    public Map<String ,Object> deleteResume(Integer resumeId){
        Map<String,Object> map = new HashMap<>();
        int res = userResumeService.deleteResume(resumeId);
        if(res == 0){
            map.put("msg","success");
            return map;
        }else {
            map.put("msg","fail");
            return map;
        }
    }

    @GetMapping("/userresume/getResume")
    public List<User_resume> getResume(){
        User_resumeExample example = new User_resumeExample();
        List<User_resume> resumes = userResumeService.selectResume(example);
        System.out.println(resumes);
        return resumes;
    }

    @PostMapping("/userresume/selectByUserId")
    public List<User_resume> selectByUserId(Integer userid){
        User_resumeExample example = new User_resumeExample();
        User_resumeExample.Criteria cri = example.createCriteria();
        cri.andUserIdEqualTo(userid);
        List<User_resume> resumes = userResumeService.selectByUserId(example);
        return resumes;
    }
}
